﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.VehicleReport.BLL;
using Yipusoft.VehicleReport.Model;

public partial class DutyLog_VehicleReportView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }

    }

    void BindData()
    {
        DateTime date = Convert.ToDateTime(Request["date"]);
        string DepartmentId = Request["DepartmentId"];
        DAL.Account_Department department = BLL.Department.GetDepartmentByID(DepartmentId);
        lblCaption.Text = department.ShortName + "【" + date.ToString("yyyy年MM月dd日") + "】车辆检查";
        string where = " DepartmentId='" + DepartmentId + "' and  DayDate='" + date.Date + "' ";

        repeater.DataSource = VehicleDayItem.GetList(where);
        repeater.DataBind();
    }
    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DateTime date = Convert.ToDateTime(Request["date"]);

            Label lblZDDOk = e.Item.FindControl("lblZDDOk") as Label;
            Label lblZhongdOk = e.Item.FindControl("lblZhongdOk") as Label;
            Label lblRemark = e.Item.FindControl("lblRemark") as Label;
            Button btnZDDOk = e.Item.FindControl("btnZDDOk") as Button;
            Button btnZDOK = e.Item.FindControl("btnZDOK") as Button;

            VehicleDayItemInfo info = e.Item.DataItem as VehicleDayItemInfo;
            MendRecordInfo Reco = MendRecord.GetDetail(info.RecordId);

            if (date.Date == DateTime.Now.Date)
            {//当前时间为今天
                if (info.IsDamaged)
                {
                    //情况说明
                    if (Reco.MendTime.Date == info.DayDate.Date)
                    {
                        lblRemark.Text = Reco.Remark;
                    }
                    else
                    {
                        lblRemark.Text = info.Remark;
                    }
                    //确认信息
                    lblZDDOk.Text = Reco.DZDOk;
                    lblZhongdOk.Text = Reco.ZhongdOk == "" ? "" : Reco.ZhongdOk + "<br/>" + Reco.FinishTime.ToString("yyyy-MM-dd") + "";

                    string UserDepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
                    if (Reco.ZhongdOk == "")//中队未确认
                    {
                        if (UserDepartmentId.Substring(4, 1) == "3")//中队
                        {
                            btnZDDOk.Visible = false;
                            btnZDOK.Visible = true;
                        }
                        else if (UserDepartmentId.Substring(4, 1) == "2" || UserDepartmentId.Substring(4, 1) == "0")//支队大队
                        {
                            btnZDDOk.Visible = true;
                            btnZDOK.Visible = false;
                        }
                        else
                        {
                            btnZDDOk.Visible = false;
                            btnZDOK.Visible = false;
                        }
                    }
                    else
                    {
                        btnZDDOk.Visible = false;
                        btnZDOK.Visible = false;
                    }
                }
                else
                {
                    lblRemark.Text = "完好";
                    btnZDDOk.Visible = false;
                    btnZDOK.Visible = false;
                    lblZDDOk.Text = "";
                    lblZhongdOk.Text = "";
                }
            }
            else
            {
                lblRemark.Text = info.Remark;
                btnZDDOk.Visible = false;
                btnZDOK.Visible = false;
                if (info.IsDamaged)
                {
                    if (Reco != null)
                    {
                        lblZDDOk.Text = Reco.DZDOk;
                        lblZhongdOk.Text = Reco.ZhongdOk == "" ? "" : Reco.ZhongdOk + "<br/>【" + Reco.FinishTime.ToString("yyyy-MM-dd") + "】";
                    }
                    else
                    {
                        lblZDDOk.Text ="";
                        lblZhongdOk.Text = "";
                    }
                }
                else
                {
                    lblZDDOk.Text = "";
                    lblZhongdOk.Text = "";
                }
                btnZDDOk.Visible = false;
                btnZDOK.Visible = false;
            }
        }
    }
    protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "ZDD")//支队大队确认
        {
            Guid RecordId = new Guid(e.CommandArgument.ToString());
            MendRecordInfo Reco = MendRecord.GetDetail(RecordId);
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            string departmentName = Yipusoft.Account.BLL.Department.GetDepartment(user.DepartmentID).ShortName;
            Reco.DZDOk += Reco.DZDOk == "" ? "" : "<br/><br/>";
            Reco.DZDOk += departmentName + "<br/>" + user.UserName + "<br/>" + DateTime.Now.ToShortDateString();
            MendRecord.Update(Reco);

        }
        if (e.CommandName == "ZD")//中队确认
        {
            Guid RecordId = new Guid(e.CommandArgument.ToString());
            MendRecordInfo Reco = MendRecord.GetDetail(RecordId);
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            Reco.ZhongdOk = user.UserName;
            Reco.FinishTime = DateTime.Now;
            MendRecord.Update(Reco);
        }
        BindData();
    }
}
